package com.mf.service.authority.impl;


import com.mf.common.sso.LoginContext;
import com.mf.dao.authority.UserInfoDao;
import com.mf.domain.authority.UserInfo;
import com.mf.domain.authority.UserPortrait;
import com.mf.domain.authority.UserRoleRelation;
import com.mf.service.authority.UserInfoService;
import com.mf.service.authority.UserPortraitService;
import com.mf.service.authority.UserRoleRelationService;
import com.mf.service.framework.GenericService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息Service层实现类
 * User: zhaoming
 * DateTime: 2016-07-14
 * To change this template use File | Settings | File Templates.
 **/
@Service
public class UserInfoServiceImpl extends GenericService<UserInfo> implements UserInfoService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private UserInfoDao userInfoDao;
    @Resource
    private UserRoleRelationService userRoleRelationservice;
    @Resource
    private UserPortraitService userPortraitService;

    /**
     * 新增用户信息
     * @param request
     * @param userInfo 用户信息Domain
     */
    @Transactional
    public boolean insert(HttpServletRequest request, UserInfo userInfo){

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("file");

        String currentUserCode = LoginContext.getLoginUser().getUserCode();
        if(multipartFile != null && multipartFile.getSize() > 0){
            UserPortrait userPortrait = userPortraitService.uploadImageFile(multipartFile, userInfo.getUserCode());
            userInfo.setUserPortraitId(userPortrait.getId());
        }

        userInfo.setPassWord(DigestUtils.md5Hex(userInfo.getPassWord()));
        userInfo.setCreateUser(currentUserCode);
        userInfo.setCreateTime(new Date());
        userInfo.setUpdateUser(currentUserCode);
        userInfo.setUpdateTime(new Date());
        return userInfoDao.insert(userInfo);
    }

    /**
     * 修改用户信息
     * @param request
     * @param userInfo 用户信息Domain
     */
    @Transactional
    public boolean updateByPrimaryKey(HttpServletRequest request, UserInfo userInfo){

        Long userPortraitId = userInfo.getUserPortraitId();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("file");

        String userCode = LoginContext.getLoginUser().getUserCode();
        if(multipartFile != null && multipartFile.getSize() > 0){
            UserPortrait userPortrait = userPortraitService.uploadImageFile(multipartFile, userInfo.getUserCode());
            userInfo.setUserPortraitId(userPortrait.getId());
        }

        if(userPortraitId != null && userPortraitId != userInfo.getUserPortraitId()){
            userPortraitService.deleteByPrimaryKey(userPortraitId);
        }

        userInfo.setUpdateUser(userCode);
        userInfo.setUpdateTime(new Date());
        return userInfoDao.updateByPrimaryKey(userInfo);
    }

    /**
     * 根据主键删除用户信息
     * @param userInfo 用户信息Domain
     */
    @Transactional
    public boolean deleteByPrimaryKey(UserInfo userInfo){
        userInfo = userInfoDao.selectByPrimaryKey(userInfo);
        //删除用户与角色映射表中该用户相关联的数据
        UserRoleRelation userRoleRelation = new UserRoleRelation();
        userRoleRelation.setUserCode(userInfo.getUserCode());
        List<UserRoleRelation> userRoleRelations = userRoleRelationservice.selectByCondition(userRoleRelation);
        userRoleRelationservice.deleteByPrimaryKeys(userRoleRelations);
        return userInfoDao.deleteByPrimaryKey(userInfo);
    }

    /**
     * 根据主键查询用户信息
     * @param userInfo 用户信息Domain
     * @return 用户信息
     */
    public UserInfo selectByPrimaryKey(UserInfo userInfo){
        userInfo = userInfoDao.selectByPrimaryKey(userInfo);
        UserPortrait userPortrait = userPortraitService.selectByPrimaryKey(userInfo.getUserPortraitId());
        userInfo.setUserPortrait(userPortrait);
        return userInfo;
    }

    /**
     * 密码变更
     * @param oldPwd 原始密码
     * @param newPwd 新密码
     */
    @Transactional
    public UserInfo changePassword(String oldPwd, String newPwd){
        String currentUserCode = LoginContext.getLoginUser().getUserCode();

        UserInfo userInfo = new UserInfo();
        userInfo.setUserCode(currentUserCode);
        userInfo = userInfoDao.selectByCondition(userInfo).get(0);
        if(DigestUtils.md5Hex(oldPwd).equals(userInfo.getPassWord())){
            userInfo.setPassWord(DigestUtils.md5Hex(newPwd));
            userInfo.setUpdateUser(currentUserCode);
            userInfo.setUpdateTime(new Date());
            userInfoDao.updateByPrimaryKey(userInfo);
        }else{
            throw new RuntimeException("原始密码不正确");
        }
        return userInfo;
    }

    /**
     * 检查重复数据
     * @return 验证结果
     */
    public Map<String, Object> checkRepeat(UserInfo userInfo){
        Map<String, Object> map = new HashMap<>();
        List<UserInfo> userInfos = userInfoDao.selectByCondition(userInfo);
        if(CollectionUtils.isNotEmpty(userInfos)){
            map.put("error", "重复数据");
        }else{
            map.put("ok", "");
        }
        return map;
    }


}
